/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/slacrey/jeesite">JeeSite</a> All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.xxlie.auth.domain;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.*;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.xxlie.auth.domain.base.BaseEntity;

/**
 * 岗位实体
 *
 * @author xxlie.com
 * @version 2013-05-15
 */
@Entity
@Table(name = "sys_role")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Role extends BaseEntity {

    private static final long serialVersionUID = 1L;
    private Long id;        // 编号
    private String name;    // 岗位名称
    private String enname;    //英文名称
    private String roleType;//权限类型
    private String dataScope; // 数据范围

    private Set<User> userList = new HashSet<>(); // 拥有用户列表
    private Set<Menu> menuList = new HashSet<>(); // 拥有菜单列表

    public Role() {
        super();
    }

    public Role(Long id, String name) {
        this();
        this.id = id;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEnname() {
        return enname;
    }

    public void setEnname(String enname) {
        this.enname = enname;
    }

    public String getRoleType() {
        return roleType;
    }

    public void setRoleType(String roleType) {
        this.roleType = roleType;
    }

    public String getDataScope() {
        return dataScope;
    }

    public void setDataScope(String dataScope) {
        this.dataScope = dataScope;
    }

    @OneToMany(mappedBy = "role", fetch = FetchType.LAZY)
    @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    @JsonIgnore
    public Set<User> getUserList() {
        return userList;
    }

    public void setUserList(Set<User> userList) {
        this.userList = userList;
    }

    @Transient
    public List<Long> getUserIdList() {
        List<Long> nameIdList = new ArrayList<>();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return nameIdList;
    }

    @Transient
    public String getUserIds() {
        List<Long> nameIdList = new ArrayList<>();
        for (User user : userList) {
            nameIdList.add(user.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "sys_role_menu", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
    @OrderBy("id")
    @Fetch(FetchMode.SUBSELECT)
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(Set<Menu> menuList) {
        this.menuList = menuList;
    }

    @Transient
    public List<Long> getMenuIdList() {
        List<Long> menuIdList = new ArrayList<>();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    @Transient
    public void setMenuIdList(List<Long> menuIdList) {
        menuList = new HashSet<>();
        for (Long menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    @Transient
    public String getMenuIds() {
        HashSet<Long> nameIdList = new HashSet<>();
        for (Menu menu : menuList) {
            nameIdList.add(menu.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    @Transient
    public void setMenuIds(String menuIds) {
        menuList = new HashSet<>();
        if (menuIds != null) {
            String[] ids = StringUtils.split(menuIds, ",");
            for (String menuId : ids) {
                Menu menu = new Menu();
                menu.setId(new Long(menuId));
                menuList.add(menu);
            }
        }
    }

    /**
     * 获取权限字符串列表
     */
    @Transient
    public Set<String> getPermissions() {
        Set<String> permissions = new HashSet<>();
        for (Menu menu : menuList) {
            if (menu.getPermission() != null && !"".equals(menu.getPermission())) {
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

    @Transient
    public boolean isAdmin() {
        return isAdmin(this.id);
    }

    @Transient
    public static boolean isAdmin(Long id) {
        return id != null && id.equals(1L);
    }

    @Transient
    public String authority() {
        return "ROLE_" + enname;
    }

}
